package com.suyang.syt.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.User;
import com.suyang.syt.model.WeixinUser;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinUserService;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/weixin/user")
@AuthClass(value = "login")
public class WeixinUserController {

	@Autowired
	private WeixinUserService weixinUserService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@AuthMethod(role = "ROLE_WEIXIN")
	public String list() {
		return "weixin/user/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public PagerEasyUI<WeixinUser> list(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			return weixinUserService.listByEasyUI(page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/sync", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj sync(HttpServletRequest request) {
		try {
			return weixinUserService.updateSync();
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/updateBatch", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj updateBatch(HttpServletRequest request, String ids) {
		try {
			return weixinUserService.updateBatch(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/updateRemark", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj updateRemark(HttpServletRequest request, String openid, String remark) {
		try {
			return weixinUserService.updateRemark(openid, remark);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/updateBatchmoveGroup", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj updateBatchmoveGroup(HttpServletRequest request, String openid_list, int to_groupid) {
		try {
			return weixinUserService.updateBatchmoveGroup(openid_list, to_groupid);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	/** 微信用户授权后改密 */
	@RequestMapping(value = "/updatePwd", method = RequestMethod.GET)
	@ResponseBody
	@AuthMethod
	public AjaxObj updatePwd(HttpServletRequest request, String passwdNew, String passwdRe) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user == null) {
				return new AjaxObj(0, "对不起,您没有被授权,不能修改密码!");
			}
			return weixinUserService.updatePwd(user, passwdNew, passwdRe);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	/** 微信用户授权后绑定 */
	@RequestMapping(value = "/updateBinding", method = RequestMethod.GET)
	@ResponseBody
	@AuthMethod
	public AjaxObj updateBinding(HttpServletRequest request, String phone, String passwd, String passwdRe) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user == null) {
				return new AjaxObj(0, "对不起,您没有被授权,不能进行绑定!");
			}
			return weixinUserService.updateBinding(user, phone, passwd, passwdRe);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
